<?php
namespace app\api\controller;

use think\Controller;

class Sms extends Controller{
    //获取手机验证码
    public function getCaptcha(){
        $besender = input('post.beSender', false);
        if($besender){
            $phone = session('user.phone');
        }else{
            $phone = request()->post('phone');
        }
        if(!empty($phone)){
            $rand = strval(mt_rand(100000,999999));
            session('captcha', $rand);
            $res = \sms\Sendsms::sendVfcode($phone, '蜂鸟驿站', 'SMS_165060929', $rand);
            if(isset($res['Code']) && $res['Code'] == 'OK'){
                return json([
                    'code' => 0,
                    'msg'  => '发送成功',
                ]);
            }else{
                return json(['code'=>-1, 'msg'=>$res]);
            }
        }else{
            return json('请输入正确的信息');
        }
    }

    /**
     * 定时任务api
     * 
     */
    public function clockTask(){
        return 'ok';
        // $order = db('send_order')->where('send_status', 0)
        // ->whereTime('create_time', '<', time() - 15 * 60)
        // ->select();
        // $or = [];
        // try {
        //     foreach ($order as $v) {
        //         $res = \sms\Sendsms::sendMessage('15351217062', '长征众行', 'SMS_165060869', ['order'=>$v['send_code']]);
        //         if(isset($res['Code']) && $res['Code'] == 'OK'){
        //             $or[] = $v['send_code'];
        //         }
        //     }
        //     db('send_order')->where('send_code', 'in', $or)->update(['send_status'=>1]);
        //     return 'ok';
        // } catch (\Throwable $th) {
        //     return 'fail';
        // }
    }

}